import busItem from './buy/BusItem.vue'
import lineInfo from './buy/LineInfo.vue'
import chooseType from './buy/ChooseType.vue'
import chooseTime from './buy/ChooseTime.vue'
import chooseTicket from './buy/ChooseTicket.vue'
import ticketItem from './buy/TicketItem.vue'
import timeBlock from './buy/TimeBlock.vue'
import choosePopulation from './buy/ChoosePopulation.vue'

import myTicketItem from './my/MyTicketItem.vue'

import recordList from './record/RecordList.vue'
import recordItem from './record/RecordItem.vue'

import footBar from './public/FootBar.vue'
import Toast from './public/Toast.vue'
import listState from './public/ListState.vue'
import loading from './public/Loading.vue'

const components = {
  install(Vue) {
    Vue.component('bus-item', busItem)
    Vue.component('foot-bar', footBar)
    Vue.component('line-info', lineInfo)
    Vue.component('choose-type', chooseType)
    Vue.component('choose-time', chooseTime)
    Vue.component('choose-ticket', chooseTicket)
    Vue.component('time-block', timeBlock)
    Vue.component('ticket-item', ticketItem)
    Vue.component('choose-population', choosePopulation)
    Vue.component('my-ticket-item', myTicketItem)
    Vue.component('record-list', recordList)
    Vue.component('record-item', recordItem)
    Vue.component('list-state', listState)
    Vue.component('loading', loading)
    Vue.prototype.showToast = msg => {
      let ToastClass = Vue.extend(Toast)
      let msgInstance = new ToastClass()
      msgInstance.$mount()
      let oldToast = document.querySelector('#' + msgInstance.$el.id)
      let dur = null
      if (oldToast) {
        // 判断当前弹窗是否已经出现，已出现内容不重复的移除旧的再创建新的，避免重复创建，重复的则不做处理
        console.log(oldToast.innerText)
        if (oldToast.innerText !== msg) {
          document.body.removeChild(oldToast)
          clearTimeout(dur)
          document.body.appendChild(msgInstance.$el)
          msgInstance.showToast(msg)
          dur = setTimeout(() => {
            document.body.removeChild(oldToast)
          }, 1200)
        }
      } else {
        document.body.appendChild(msgInstance.$el)
        msgInstance.showToast(msg)
        dur = setTimeout(() => {
          document.body.removeChild(msgInstance.$el)
        }, 1200)
      }
    }
  }
}
// 导出模块
export default components
